<?php
//mysql 备份
class Acmd_mysql{
	function  __construct() {
		global $_SERVER;
		$this->argv = $_SERVER["argv"];//各参数的值(array)
		if(!empty($this->argv[3])){
			$dbconfig = $this->argv[3];
		}else{
			$dbconfig = "localhost|au|1002|abans_web|utf8";//默认数据库连接
		}
		$db = explode('|',$dbconfig);
		define('CMD_db_name', $db[3]);
		$this->pdb = Afun_pdo('mysql',$db[0],$db[1],$db[2],$db[3],$db[4]);
		$this->db = $db;
	}
	function arun($argv){
		switch ($argv[2]){
			case 'backupall':
				$this->mysql_backupall();//备份整个数据库
				$this->mysql_backup_zip();//压缩储存并删除sql文件
				break;
			case 'backup'://备份数据库中的一个表
				$this->mysql_backup($argv[4]);
				break;
			case 'backdelall'://备份数据库中的一个表并删除数据
				$this->mysql_backdelall($argv[4]);
				break;
			case 'backdel'://备份数据库中的一个表并删除数据
				$this->mysql_backdel($argv[4]);
				break;
			case 'insertall'://恢复数据库中的一个表
			//	$this->mysql_backup_unzip($argv[4]);//解压备份文件
				$this->mysql_insertall($argv[4]);
				break;
			case 'insert'://恢复数据库中的一个表
				$this->mysql_insert($argv[4]);
				break;
		}
	}



//--备份所有表
	function mysql_backupall(){
		$tablename = $this->mysql_show_tablename();
		foreach($tablename as $key=>$val){
			$this->mysql_backup($val);
		}
	}
//--压缩备份的数据文件 并删除备份的sql文件
	function mysql_backup_zip(){
		$path = $this->mysql_bakup_path();
		$pathinfo = pathinfo($path);
		$zip = Afun_acan_new("zip");
		$newzip = $zip->New_zip($pathinfo['dirname'].'/'.$pathinfo['basename'].'.zip');
		$newzip->create($path,PCLZIP_OPT_REMOVE_PATH, $path,PCLZIP_OPT_ADD_PATH, $pathinfo['basename']);
		Afunc_file()->armdirr($path);
		Afun_cmd_echo("压缩成功".$pathinfo['basename']);
	}
	
//--解压备份文件
	function mysql_backup_unzip($data){
		$path = $this->mysql_bakup_path($data);
		$pathinfo = pathinfo($path);
		$zip = Afun_acan_new("zip")->New_zip($path.'.zip');
		if ($zip->extract(PCLZIP_OPT_PATH,$pathinfo['dirname']) == 0) {
			die("Error : ".$zip->errorInfo(true));
		}
		Afun_cmd_echo("解压成功".$pathinfo['basename']);
	}
//--恢复所有表的数据
	function mysql_insertall($data){
		$path = $this->mysql_bakup_path($data);
		$afile = Afunc_file();
		$dir_list = $afile->ascandir($path.'/');
		foreach($dir_list['file'] as $key=>$val){
			$this->mysql_insert($path.'/'.$val);
			Afun_cmd_echo("还原成功".$val);
		}
		Afun_cmd_echo("还原成功".$data);
	}
//--恢复单个表
	function mysql_insert($path){
		$content = file_get_contents($path);//获取内容
		$this->pdb->exec($content);
	}

//--备份指定表
	function mysql_backup($tablename){
		$path = $this->mysql_bakup_path().'/'.$tablename.'.sql';
		Afunc_file()->aunlink($path);//删除文件
		$field = $this->mysql_insert_field($tablename);
		$counts = $this->pdb->query("SELECT count(*) FROM $tablename")->fetch();
		$model = $this->pdb->query("SELECT * FROM $tablename");
		if($counts[0] == '0'){
			Afun_cmd_echo("没有数据".$tablename);
			return true;
		}
		for($m=0;$m<$counts[0];$m++){
			$val = $model->fetch();
			for($i=0;$i<$field['count'];$i++){
				if(empty($value)){
					$value = Afun_mysql_is_null($val[$i]);
				}else{
					$value .= ",".Afun_mysql_is_null($val[$i]);
				}
			}
			$values = $field['into']."(".$value.");\r\n";
			Afunc_file()->aedit_save($path,$values,$charset="utf8","a+");//写入
			unset($value,$values);
		}
		Afun_cmd_echo("备份完成".$tablename);
	}

//创建备份路径
	function mysql_bakup_path($data = null){
		if($data==null){
			$data = date("Y-m-d",time());
		}
		$path = A_ROOT.'adata/bakup/mysql/'.CMD_db_name.'/'.$data;
		Afunc_file()->amkdirm($path);
		return realpath($path);
	}

//--返回INSERT语句
	function mysql_insert_field($tablename){
		$row_table = $this->pdb->query("SHOW COLUMNS FROM $tablename")->fetchAll();
		foreach($row_table as $key=>$val){
			$field[] = $val['Field'];
			if(empty($into)){
				$into = '`'.$val['Field'].'`';
			}else{
				$into .= ',`'.$val['Field'].'`';
			}
		}
		$rs['field'] = $field;
		$rs['count'] = count($field);
		$rs['into'] = 'INSERT INTO `'.$tablename.'` ('.$into.') VALUES ';
		return $rs;
		
	}
//--列出数据库的所有表名
	function mysql_show_tablename(){
		$mdb = Afun_mysql_link($this->db[0],$this->db[1],$this->db[2],$this->db[3],$this->db[4]);
		$query = mysql_query("SHOW TABLE STATUS FROM abans_web");
		$row_table = mysql_num_rows($query);
		for($i=1;$i<=$row_table;$i++){
			$row = mysql_fetch_array($query);
			$tablename[] = $row['Name'];
		}
		return $tablename;
	}
}




?>